#include "SDL2_wip.c"
#include "SDL2_wip.h"
#include "SDL_render.h"
#include "SDL_stdinc.h"
#include "SDL_video.h"
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

int
circle_of_lines (SDL_Renderer *renderer, Uint8 fg, Uint8 bg, int midx, int midy,
                 int rad_in, int rad_out, int iter, float thick_start,
                 float thick_step)
{
  filledRectRaw (renderer, midx - rad_out, midy - rad_out, midx + rad_out,
                 midy + rad_out, bg, bg, bg, 255);
  float rad_i = rad_in;
  float rad_o = rad_out;
  float thickness = thick_start;
  for (float i = 0; i < 6.28; i = i + 6.28 / iter)
    {
      float x1 = rad_i * cos (i);
      float y1 = rad_i * sin (i);
      float x2 = rad_o * cos (i);
      float y2 = rad_o * sin (i);
      line (renderer, x1 + midx, y1 + midy, x2 + midx, y2 + midy, thickness, 0,
            fg, fg, fg, 255);
      thickness += thick_step;
    }
  return 0;
}

int
drawRegPolyTest (SDL_Renderer *renderer)
{

  float cx = 400;
  float cy = 400;

  float rad = 200;

  int num_sides = 5;

  regularPolygon (renderer, cx, cy, rad, num_sides, 20, 0, 255, 0, 0, 0, 0, 255,
                  255, 255, 255);
}

int
drawFanGeometryTest (SDL_Renderer *renderer)
{
  float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

  x1 = 50;
  y1 = 50;

  x2 = 700;
  y2 = 50;

  x3 = 700;
  y3 = 700;

  x4 = 100;
  y4 = 600;

  x5 = 200;
  y5 = 700;

#define num_vert 4

  float x_arr[num_vert] = { x1, x2, x3, x4 };
  float y_arr[num_vert] = { y1, y2, y3, y4 };

  _renderFanPolygon (renderer, 400, 400, x_arr, y_arr, num_vert, 255, 255, 255,
                     255);
}

int
drawAARectTest (SDL_Renderer *renderer)
{

  float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

  x1 = 50;
  y1 = 50;

  x2 = 700;
  y2 = 700;

  float thickness = 30.0;

  aaRect (renderer, x1, y1, x2, y2, 0, 0, 255, 0, 255, 255, 1, 255, 255, 255,
          255);
  return 0;
}

int
drawPolylineTest (SDL_Renderer *renderer)
{
  float x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

  x1 = 50;
  y1 = 400;

  x2 = 100;
  y2 = 250;

  x3 = 400;
  y3 = 20;

  x4 = 200;
  y4 = 400;

  x5 = 700;
  y5 = 700;

  float w = 3;

#define poly_num_vert 6

  float x_arr[poly_num_vert] = { x1, x2, x3, x4, x5, x1 };
  float y_arr[poly_num_vert] = { y1, y2, y3, y4, y5, y1 };

  polyline (renderer, x_arr, y_arr, num_vert, 2.0, 0, 0, 255, 255, 255, 255);
  if (0)
    {

      rectRaw (renderer, x1 - w, y1 - w, x1 + w, y1 + w, 255, 0, 0, 255);
      rectRaw (renderer, x2 - w, y2 - w, x2 + w, y2 + w, 255, 0, 0, 255);
      rectRaw (renderer, x3 - w, y3 - w, x3 + w, y3 + w, 255, 0, 0, 255);
      rectRaw (renderer, x4 - w, y4 - w, x4 + w, y4 + w, 255, 0, 0, 255);
    }
  return 0;
}

int
draw (SDL_Renderer *renderer)
{
  // circle_of_lines (renderer, 255, 0, 400, 400, 40, 300, 40, 1, 0.2);
  //  circle_of_lines (renderer, 0, 255, 600, 200, 40, 200, 16, 0.6 * 20, 0.6);
  //    circle_of_lines (renderer, 255, 0, 200, 600, 100, 200, 14, 0.6 *
  //    36, 1.2); circle_of_lines (renderer, 255, 0, 600, 600, 120, 200, 10, 0.6
  //    * 60 + 1.2 * 14, 2.4);

  // drawPolylineTest (renderer);
  // drawAARectTest (renderer);
  // drawFanGeometryTest (renderer);
  drawRegPolyTest (renderer);
}

int
main (int argc, char **argv)
{
  SDL_SetHint (SDL_HINT_RENDER_SCALE_QUALITY, "2");
  SDL_SetHint (SDL_HINT_RENDER_LINE_METHOD, "3");
  SDL_SetHint (SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  SDL_Init (SDL_INIT_VIDEO);
  SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
  SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 16);

  SDL_Window *win = SDL_CreateWindow ("Rendering Geometry", 0, 0, 800, 800,
                                      SDL_WINDOW_ALLOW_HIGHDPI);
  if (!win)
    {
      SDL_Quit ();
      return 2;
    }

  SDL_Renderer *renderer
      = SDL_CreateRenderer (win, -1, SDL_RENDERER_ACCELERATED);
  if (!renderer)
    {
      SDL_DestroyWindow (win);
      return 3;
    }
  int render_w, render_h;
  int window_w, window_h;
  float scale_x, scale_y;
  SDL_GetRendererOutputSize (renderer, &render_w, &render_h);
  SDL_GetWindowSize (win, &window_w, &window_h);
  scale_x = (float)(render_w) / (float)(window_w);
  scale_y = (float)(render_h) / (float)(window_h);
  printf ("%.6f", scale_x);
  printf ("%.6f", scale_y);
  SDL_RenderSetScale (renderer, scale_x, scale_y);

  SDL_bool running = SDL_TRUE;
  SDL_Event ev;
  while (running)
    {
      while (SDL_PollEvent (&ev))
        {
          if (ev.type == SDL_QUIT)
            {
              running = SDL_FALSE;
            }
        }

      SDL_SetRenderDrawColor (renderer, 0, 0, 0, 255);
      SDL_RenderClear (renderer);
      draw (renderer);
      SDL_RenderPresent (renderer);
    }

  SDL_DestroyRenderer (renderer);
  SDL_DestroyWindow (win);
  SDL_Quit ();
  return 0;
}
